﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class NewProjectForm : Form
    {
        Controller controller = null;
        Project dataObject = null;
        ProjectsForm cForm = null;

        public NewProjectForm(ProjectsForm cForm)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterProject());
            dataObject = new Project();
            loadCustomerList();
            cbx_client.SelectedIndex = 0;
            cbx_status.SelectedIndex = 0;
        }

        public NewProjectForm(ProjectsForm cForm, Project dataObject)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterProject());
            this.dataObject = dataObject;
            tbx_name.Text = dataObject.Name;
            loadCustomerList();
            int i = 0;
            foreach (Customer temp in cbx_client.Items)
            {
                if (dataObject.Customer.Id == temp.Id)
                {
                    cbx_client.SelectedIndex = i;
                }
                i++;
            }
            date_deadline.Text = Convert.ToString(dataObject.DeadLine);
            int e = 0;
            foreach (String temp in cbx_status.Items)
            {
                if (dataObject.Status == temp)
                {
                    cbx_status.SelectedIndex = e;
                }
                e++;
            }
        }

        private void loadCustomerList()
        {
            foreach (List<object> tempList in new Controller(new MYSQLAdabterCustomer()).Select())
            {
                Customer temp = new Customer();
                temp.Id = (int)tempList[0];
                temp.Name = (String)tempList[1];
                temp.Adress = (String)tempList[2];
                temp.ZIP = (int)tempList[3];
                temp.Country = (String)tempList[4];
                cbx_client.Items.Add(temp);
            }
            cbx_client.DisplayMember = "Name";
            cbx_client.ValueMember = "Id";
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            Boolean failed = false;

            if(tbx_name.Text == ""){
                lbl_name_error.Visible = true;
                failed = true;
            }
            if (failed) { return; }

            dataObject.Name = tbx_name.Text;
            Customer tempCustomer = (Customer)cbx_client.SelectedItem;
            dataObject.Customer = tempCustomer;
            dataObject.DeadLine = date_deadline.Value;
            dataObject.Status = (String)cbx_status.SelectedItem;
            controller.Save(dataObject);
            cForm.dataLoad();
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            cForm.dataLoad();
            this.Close();
        }
    }
}
